<%--
  Created by IntelliJ IDEA.
  User: pjh19
  Date: 15/8/2021
  Time: 上午11:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
%>
<html>
<head>
    <title>Title</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery-3.1.1.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/bootstrap4.6.0/js/bootstrap.js"></script>
    <link href="<%=path%>/static/bootstrap4.6.0/css/bootstrap.css" rel="stylesheet">
    <style type="text/css">
        img{width:300px;height:300px;}
    </style>
</head>
<body>
<div class="container">
    <h3>商品展示页面</h3><a class="btn btn-success" href="${pageContext.request.contextPath}/goods/getAllByPage">返回商品页面</a>
    <div class="container">
        <img src="${pageContext.request.contextPath}${goods.pImage}.png"><br>
        ${goods.pDesc}<br>

        <h3>价格:${goods.price}</h3>
        <button class="btn btn-warning" onclick="gotoshpping(${sessionScope.loginUser.userId},${goods.goodsId},${goods.price},'${sessionScope.loginUser.userName}')">购买</button>
        <button class="btn btn-danger" onclick="gotocart(${sessionScope.loginUser.userId},${goods.goodsId})">
            <svg class="bi bi-cart-plus" width="2em" height="2em" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                <path fill-rule="evenodd" d="M8.5 5a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1H8V5.5a.5.5 0 0 1 .5-.5z"/>
                <path fill-rule="evenodd" d="M8 7.5a.5.5 0 0 1 .5-.5h2a.5.5 0 0 1 0 1H9v1.5a.5.5 0 0 1-1 0v-2z"/>
                <path fill-rule="evenodd" d="M0 1.5A.5.5 0 0 1 .5 1H2a.5.5 0 0 1 .485.379L2.89 3H14.5a.5.5 0 0 1 .491.592l-1.5 8A.5.5 0 0 1 13 12H4a.5.5 0 0 1-.491-.408L2.01 3.607 1.61 2H.5a.5.5 0 0 1-.5-.5zM3.102 4l1.313 7h8.17l1.313-7H3.102zM5 12a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm7 0a2 2 0 1 0 0 4 2 2 0 0 0 0-4zm-7 1a1 1 0 1 0 0 2 1 1 0 0 0 0-2zm7 0a1 1 0 1 0 0 2 1 1 0 0 0 0-2z"/>
            </svg>
        </button>

    </div>

</div>
<script>
    function gotoshpping(userid,goodsId,price,userName) {
        var userId =userid;
        var goodsId =goodsId;
        var price =price;
        var name = userName;
        console.log(userId,goodsId,price,name)
        window.location.href=
            "${pageContext.request.contextPath}/user/shop/createOrderByGoods?userId="+userId+"&total="+price+"&goodsId="+goodsId+"&name="+name
    }
    function gotocart(userId,goodsId) {
        var userId =userId;
        var goodsId =goodsId;
        console.log(userId,goodsId)
        $.ajax({
            url:"${pageContext.request.contextPath}/user/shop/addCart",
            type:"post",
            contentType:"application/x-www-form-urlencoded",
            data:{userId:userId,goodsId:goodsId},
            dataType: "json",
            success:function (result) {
                alert(JSON.stringify(result))
            },
            error:function (err) {
                alert(err)
            }
        })
    }
</script>

</body>
</html>
